<?php

/**
 * 导入视频课程小节
 */

namespace App\Imports;

use App\Models\LearnVideo;
use App\Models\LearnVideoChapter;
use App\Models\VideoLibrary;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\ToCollection;

class LearnVideoSectionImport implements ToCollection
{

    protected int $learnVideoId;
    protected int $learnVideoChapterId;

    public function __construct($learnVideoId, $learnVideoChapterId)
    {
        $this->learnVideoId = $learnVideoId;
        $this->learnVideoChapterId = $learnVideoChapterId;
    }

    /**
     * @param Collection $collection
     * @throws \Throwable
     */
    public function collection(Collection $collection)
    {
        //如果需要去除表头
        unset($collection[0]);
        //$rows 是数组格式
        $this->createData($collection);
    }

    /**
     * @param $collection
     * @return void
     * @throws \Throwable
     */
    public function createData($collection)
    {
        DB::transaction(function () use ($collection) {
            $successCount = 0;
            $successTotalSecond = 0;
            $successIds = [];

            // 获取最后一条数据的sort
            $lastSort = DB::table('learn_video_sections')
                ->where('learn_video_id', $this->learnVideoId)
                ->where('learn_video_chapter_id', $this->learnVideoChapterId)
                ->value('sort') ?? 0;

            foreach ($collection as $k=>$item) {
                $fileFormat = !empty($item[4]) ? $item[4] : 'mp4';

                $_id = DB::table('learn_video_sections')->insertGetId([
                    'learn_video_id' => $this->learnVideoId,
                    'learn_video_chapter_id' => $this->learnVideoChapterId,
                    'name' => $item[0],
                    'total_size' => $item[2],
                    'total_second' => $item[3],
                    'video_format' => $fileFormat,
                    'video_url' => $item[1],
                    'sort' => $lastSort+$k,
                ]);
                if ($_id) {
                    $successCount += 1;
                    $successTotalSecond += $item[3];
                    $successIds[] = $_id;
                }

                // 导入视频库
                VideoLibrary::create([
                    'name' => $item[0],
                    'total_size' => $item[2],
                    'total_second' => $item[3],
                    'video_format' => $fileFormat,
                    'video_url' => $item[1],
                ]);
            }

            // 更新chapter数量
            // 更新learnVideo数量
            $learnVideo = LearnVideo::find($this->learnVideoId);
            $learVideoChapter = LearnVideoChapter::find($this->learnVideoChapterId);
            $learnVideo->total_second += $successTotalSecond;
            $learnVideo->section_count += $successCount;
            $learVideoChapter->total_second += $successTotalSecond;
            $learVideoChapter->section_count += $successCount;
            $learnVideo->save();
            $learVideoChapter->save();


            Log::info('视频课程EXCEL导入('.$successCount.'个)', $successIds);
        });
    }
}
